//
//  EmoticonPageCell.swift
//  EmoticonKeyboard
//
//  Created by HM on 16/5/29.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class EmoticonPageCell: UICollectionViewCell {
    
    var emoticons: [KKEmoticon]? {
        didSet {
        //绑定数据
        //获取所有按钮
        //给所有按钮设置数据模型
            for (index,emoticon) in (emoticons?.enumerate())! {
                // 获取每一个模型
                let btn = buttons[index]
                btn.emoticon = emoticon
                
            }

        }
        
    }
    //定义按钮的数组
    private lazy var buttons: [KKEmoticonButton] = [KKEmoticonButton]()


    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        contentView.addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) in
            make.center.equalTo(contentView.snp_center)
        }
        
        //添加显示表情图片的控件  -> UIButton * 21
        let margin: CGFloat = 5
        //定义距离底部的距离
        let bottomMargin: CGFloat = 30
        let itemW: CGFloat = (UIScreen.mainScreen().bounds.width - 2 * margin) / CGFloat(EmoticonColCount)
        let itemH = (self.bounds.height - bottomMargin) / CGFloat(EmoticonRowCount)
        
        for i in 0..<21 {
            let btn = KKEmoticonButton()

//            btn.setTitle("王大锤", forState: .Normal)
            
            //设置按钮的frame   -> 九宫格布局
            let row = i / EmoticonColCount
            let col = i % EmoticonColCount
            let x = margin + itemW * CGFloat(col)
            let y = CGFloat(row) * itemH
            btn.frame = CGRect(x: x, y: y, width: itemW, height: itemH)
            btn.titleLabel?.font = UIFont.systemFontOfSize(32)
            //添加按钮的点击事件
            btn.addTarget(self, action: #selector(EmoticonPageCell.btnDidClick(_:)), forControlEvents: .TouchUpInside)
            //添加到父视图上
            self.contentView.addSubview(btn)
            // 添加到数组中
            buttons.append(btn)
        }
        
        //添加手势
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(EmoticonPageCell.longPressFire(_:)))
        self.contentView.addGestureRecognizer(longPress)
    }
    @objc private func btnDidClick(btn: KKEmoticonButton) {
        //print(btn.emoticon)
        //在textView文本框中需要显示图片 图片包含在模型中
        NSNotificationCenter.defaultCenter().postNotificationName(EmoticonBtnDidClickNotification, object: btn.emoticon)
    }

    //MARK: 根据手势触摸的点 获取对应的按钮
    private func buttonWithPoint(point: CGPoint) -> KKEmoticonButton? {
        //遍历按钮的数组
        for btn in buttons {
            //根据 按钮的frame 是否包含了触摸点
            if btn.frame.contains(point) {
                return btn
            }
        }
        return nil
    }
    
    @objc private func longPressFire(ges: UILongPressGestureRecognizer) {
        let point = ges.locationInView(contentView)
        //        print(point)
        //根据手势触摸的点,找到对应的按钮
        guard let btn = buttonWithPoint(point) else {
            popView.dismissAfter()
            return
        }
        //如果手势在 删除或者空白的按钮上不添加pop
        if btn.emoticon!.isDelete || btn.emoticon!.isEmpty {
            popView.dismissAfter()
            return
        }
        
        switch ges.state {
        case .Began,.Changed:
            
            popView.show(btn)
            
        default:
            popView.dismissAfter()
        }

        
    }
    //懒加载控件
    private lazy var popView: KKEmoticonPopView = {
        let pop = KKEmoticonPopView.loadPopView()
        
        return pop
    }()

    private lazy var testLabel: UILabel = {
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(20)
        l.sizeToFit()
        return l
    }()
}
